//
//  NSString+DeviceInfo.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "NSString+DeviceInfo.h"
#import "sys/utsname.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
#import <WebKit/WebKit.h>

static NSString *kTTWebViewDefaultWebUserAgent = @"kTTWebViewDefaultWebUserAgent";

@implementation NSString (DeviceInfo)

+(void)load{
    [self getNewUserAgent];
}

+ (NSString*)getDeviceName{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)[[UIDevice currentDevice] name], NULL, (CFStringRef)@"!*'();:@&=+ $,./?%#[]", kCFStringEncodingUTF8));
    return encodedString;
#pragma clang diagnostic pop
}

+ (NSString*)getSystemName{
    return [[UIDevice currentDevice] systemName];
}

+ (NSString*)getsystemVersion{
    return [[UIDevice currentDevice] systemVersion];
}

+(NSString *)getdeviceRealName{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *machineString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    return machineString;
}
+ (NSString*)getdeviceModelName{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *machineString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    if ([machineString isEqualToString:@"iPhone1,1"])   return @"iPhone 1G";
    if ([machineString isEqualToString:@"iPhone1,2"])   return @"iPhone 3G";
    if ([machineString isEqualToString:@"iPhone2,1"])   return @"iPhone 3GS";
    if ([machineString isEqualToString:@"iPhone3,1"])   return @"iPhone 4";
    if ([machineString isEqualToString:@"iPhone3,3"])   return @"Verizon iPhone 4";
    if ([machineString isEqualToString:@"iPhone4,1"])   return @"iPhone 4S";
    if ([machineString isEqualToString:@"iPhone5,1"])   return @"iPhone 5 (GSM)";
    if ([machineString isEqualToString:@"iPhone5,2"])   return @"iPhone 5 (CDMA)";
    if ([machineString isEqualToString:@"iPhone5,3"])   return @"iPhone 5C (GSM)";
    if ([machineString isEqualToString:@"iPhone5,4"])   return @"iPhone 5C (GSM+CDMA)";
    if ([machineString isEqualToString:@"iPhone6,1"])   return @"iPhone 5S (GSM)";
    if ([machineString isEqualToString:@"iPhone6,2"])   return @"iPhone 5S (GSM+CDMA)";
    if ([machineString isEqualToString:@"iPhone7,2"])   return @"iPhone6";
    if ([machineString isEqualToString:@"iPhone7,1"])   return @"iPhone 6 Plus";
    if ([machineString isEqualToString:@"iPhone8,1"])   return @"iPhone 6 S";
    if ([machineString isEqualToString:@"iPhone8,2"])   return @"iPhone 6S Plus";
    if ([machineString isEqualToString:@"iPhone8,4"])   return @"iPhone SE";
    // 日行两款手机型号均为日本独占，可能使用索尼FeliCa支付方案而不是苹果支付
    if ([machineString isEqualToString:@"iPhone9,1"])   return @"iPhone 7";
    if ([machineString isEqualToString:@"iPhone9,2"])   return @"iPhone 7 Plus";
    if ([machineString isEqualToString:@"iPhone9,3"])   return @"iPhone 7";
    if ([machineString isEqualToString:@"iPhone9,4"])   return @"iPhone 7 Plus";
    if ([machineString isEqualToString:@"iPhone10,1"])  return @"iPhone 8";
    if ([machineString isEqualToString:@"iPhone10,4"])  return @"iPhone 8";
    if ([machineString isEqualToString:@"iPhone10,2"])  return @"iPhone 8 Plus";
    if ([machineString isEqualToString:@"iPhone10,5"])  return @"iPhone 8 Plus";
    if ([machineString isEqualToString:@"iPhone10,3"])  return @"iPhone X";
    if ([machineString isEqualToString:@"iPhone10,6"])  return @"iPhone X";
    if ([machineString isEqualToString:@"iPhone11,2"])  return @"iPhone XS";
    if ([machineString isEqualToString:@"iPhone11,4"])  return @"iPhone XS Max";
    if ([machineString isEqualToString:@"iPhone11,6"])  return @"iPhone XS Max";
    if ([machineString isEqualToString:@"iPhone11,8"])  return @"iPhone XR";
    if ([machineString isEqualToString:@"iPhone12,1"])  return @"iPhone 11";
    if ([machineString isEqualToString:@"iPhone12,3"])  return @"iPhone 11 Pro";
    if ([machineString isEqualToString:@"iPhone12,5"])  return @"iPhone 11 Pro Max";
    if ([machineString isEqualToString:@"iPhone12,8"])  return @"iPhone SE 2";
    if ([machineString isEqualToString:@"iPhone13,1"])  return @"iPhone 12 mini";
    if ([machineString isEqualToString:@"iPhone13,2"])  return @"iPhone 12";
    if ([machineString isEqualToString:@"iPhone13,3"])  return @"iPhone 12 Pro";
    if ([machineString isEqualToString:@"iPhone13,4"])  return @"iPhone 12 Pro Max";
    if ([machineString isEqualToString:@"iPhone14,5"])  return @"iPhone 13";
    if ([machineString isEqualToString:@"iPhone14,2"])  return @"iPhone 13 Pro";
    if ([machineString isEqualToString:@"iPhone14,3"])  return @"iPhone 13 Pro Max";
    if ([machineString isEqualToString:@"iPhone14,4"])  return @"iPhone 13 mini";


    if ([machineString isEqualToString:@"iPod1,1"])     return @"iPod Touch 1G";
    if ([machineString isEqualToString:@"iPod2,1"])     return @"iPod Touch 2G";
    if ([machineString isEqualToString:@"iPod3,1"])     return @"iPod Touch 3G";
    if ([machineString isEqualToString:@"iPod4,1"])     return @"iPod Touch 4G";
    if ([machineString isEqualToString:@"iPod5,1"])     return @"iPod Touch 5(Gen)";
    if ([machineString isEqualToString:@"iPod7,1"])     return @"iPod Touch 6G";
    
    if ([machineString isEqualToString:@"iPad1,1"])     return @"iPad 1";
    if ([machineString isEqualToString:@"iPad1,2"])     return @"iPad 3G";
    if ([machineString isEqualToString:@"iPad2,1"])     return @"iPad 2 (WiFi)";
    if ([machineString isEqualToString:@"iPad2,2"])     return @"iPad 2 (GSM)";
    if ([machineString isEqualToString:@"iPad2,3"])     return @"iPad 2 (CDMA)";
    if ([machineString isEqualToString:@"iPad2,4"])     return @"iPad 2 (CDMA)";
    if ([machineString isEqualToString:@"iPad2,5"])     return @"iPad Mini (WiFi)";
    if ([machineString isEqualToString:@"iPad2,6"])     return @"iPad Mini (GSM)";
    if ([machineString isEqualToString:@"iPad2,7"])     return @"iPad Mini (CDMA)";
    if ([machineString isEqualToString:@"iPad3,1"])     return @"iPad 3 (WiFi)";
    if ([machineString isEqualToString:@"iPad3,2"])     return @"iPad 3 (GSM)";
    if ([machineString isEqualToString:@"iPad3,3"])     return @"iPad 3 (CDMA)";
    if ([machineString isEqualToString:@"iPad3,4"])     return @"iPad 4 (WiFi)";
    if ([machineString isEqualToString:@"iPad3,5"])     return @"iPad 4 (GSM)";
    if ([machineString isEqualToString:@"iPad3,6"])     return @"iPad 4 (CDMA)";
    if ([machineString isEqualToString:@"iPad4,1"])     return @"iPad Air";
    if ([machineString isEqualToString:@"iPad4,2"])     return @"iPad Air (Cellular)";
    if ([machineString isEqualToString:@"iPad4,4"])     return @"iPad Mini 2";
    if ([machineString isEqualToString:@"iPad4,5"])     return @"iPad Mini 2 (Cellular)";
    if ([machineString isEqualToString:@"iPad4,7"])     return @"iPad Mini 3 (WiFi)";
    if ([machineString isEqualToString:@"iPad4,8"])     return @"iPad Mini 3 (Cellular)";
    if ([machineString isEqualToString:@"iPad4,9"])     return @"iPad Mini 3 (Cellular)";
    if ([machineString isEqualToString:@"iPad5,1"])     return @"iPad Mini 4 (WiFi)";
    if ([machineString isEqualToString:@"iPad5,2"])     return @"iPad Mini 4 (Cellular)";
    
    if ([machineString isEqualToString:@"iPad5,3"])     return @"iPad Air 2 (WiFi)";
    if ([machineString isEqualToString:@"iPad5,4"])     return @"iPad Air 2 (Cellular)";
    if ([machineString isEqualToString:@"iPad6,3"])     return @"iPad Pro 9.7 inch(WiFi)";
    if ([machineString isEqualToString:@"iPad6,4"])     return @"iPad Pro 9.7 inch(Cellular)";
    if ([machineString isEqualToString:@"iPad6,7"])     return @"iPad Pro 12.9 inch(WiFi)";
    if ([machineString isEqualToString:@"iPad6,8"])     return @"iPad Pro 12.9 inch(Cellular)";
    
    if ([machineString isEqualToString:@"iPad6,11"])    return @"iPad 5(WiFi)";
    if ([machineString isEqualToString:@"iPad6,12"])    return @"iPad 5(Cellular)";
    if ([machineString isEqualToString:@"iPad7,1"])     return @"iPad Pro 12.9 inch(2nd generation)(WiFi)";
    if ([machineString isEqualToString:@"iPad7,2"])     return @"iPad Pro 12.9 inch(2nd generation)(Cellular)";
    if ([machineString isEqualToString:@"iPad7,3"])     return @"iPad Pro 10.5 inch(WiFi)";
    if ([machineString isEqualToString:@"iPad7,4"])     return @"iPad Pro 10.5 inch(Cellular)";
    
    if ([machineString isEqualToString:@"AppleTV2,1"])  return @"appleTV2";
    if ([machineString isEqualToString:@"AppleTV3,1"])  return @"appleTV3";
    if ([machineString isEqualToString:@"AppleTV3,2"])  return @"appleTV3";
    if ([machineString isEqualToString:@"AppleTV5,3"])  return @"appleTV4";
    
    if ([machineString isEqualToString:@"i386"])        return @"i386Simulator";
    if ([machineString isEqualToString:@"x86_64"])      return @"x86_64Simulator";
    return @"Unknown";
}

+ (NSString *)getCarrierName{
    static CTTelephonyNetworkInfo *telephonyInfo;
    static CTCarrier *carrier;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        telephonyInfo = [[CTTelephonyNetworkInfo alloc] init];
        carrier = [telephonyInfo subscriberCellularProvider];
    });
    if (!carrier) return @"T";
    NSString *currentCountry = [carrier carrierName]?:@"";
    if ([currentCountry isEqualToString:@"中国移动"]) {
        return @"M";
    }else if ([currentCountry isEqualToString:@"中国联通"]) {
        return @"U";
    }else{
        return @"T";
    }
}
+ (NSString *)getNetworkType{
    AFNetworkReachabilityStatus status = [[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus];
    static CTTelephonyNetworkInfo *networkInfo;
    static NSDictionary *dic;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        networkInfo = [[CTTelephonyNetworkInfo alloc] init];
        dic = @{CTRadioAccessTechnologyGPRS  : @"GPRS",          // 2.5G   171Kbps
                CTRadioAccessTechnologyEdge  : @"2.75GEdge",     // 2.75G  384Kbps
                CTRadioAccessTechnologyWCDMA : @"3G",            // 3G  3.6Mbps/384Kbps
                CTRadioAccessTechnologyHSDPA : @"3.5G HSDPA",    // 3.5G   14.4Mbps/384Kbps
                CTRadioAccessTechnologyHSUPA : @"3.5G HSUPA",    // 3.75G  14.4Mbps/5.76Mbps
                CTRadioAccessTechnologyCDMA1x : @"3G",           // 2.5G
                CTRadioAccessTechnologyCDMAEVDORev0 : @"3G",
                CTRadioAccessTechnologyCDMAEVDORevA : @"3G",
                CTRadioAccessTechnologyCDMAEVDORevB : @"3G",
                CTRadioAccessTechnologyeHRPD : @"HRPD",
                CTRadioAccessTechnologyLTE: @"4G"}; // LTE:3.9G 150M/75M  LTE-Advanced:4G 300M/150M
    });
    switch (status) {
        case AFNetworkReachabilityStatusUnknown:
            return @"unknown";
            break;
        case AFNetworkReachabilityStatusNotReachable:
            return @"noNetwork";
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:{
            if (!networkInfo) return @"unknown";
            NSString *status = networkInfo.currentRadioAccessTechnology;
            if (!networkInfo.currentRadioAccessTechnology) return @"unknown";
            NSString *num = dic[status];
            if (num) return num;
        }
           break;
        case AFNetworkReachabilityStatusReachableViaWiFi:
            return @"wifi";
        break;
    }
    return @"unknown";
}

+(NSString *)getUserAgent{
    return [[NSUserDefaults standardUserDefaults] valueForKey:kTTWebViewDefaultWebUserAgent];
}
//此方法只负责首次加载后存到本地
+ (void)getNewUserAgent{
    if ([[NSUserDefaults standardUserDefaults] valueForKey:kTTWebViewDefaultWebUserAgent]) return;
    static dispatch_once_t onceToken;
//    NSString *kTTWebViewDefaultWebUserAgent = @"aaaaaa";
     dispatch_once(&onceToken, ^{
        static WKWebView *webView = nil;
        webView = [[WKWebView alloc] init];
        [webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id
                                                                               _Nullable result, NSError * _Nullable error) {
            [[NSUserDefaults standardUserDefaults] setObject:result forKey:kTTWebViewDefaultWebUserAgent];
            webView = nil;
        }];
    });
}

@end
